package com.example.prison.mapper;

import com.example.prison.entity.HistoricalAlarmData;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @author jiang
* @description 针对表【historical_alarm_data】的数据库操作Mapper
* @createDate 2024-01-17 15:49:31
* @Entity com.example.prison.entity.HistoricalAlarmData
*/
@Mapper
public interface HistoricalAlarmDataMapper extends BaseMapper<HistoricalAlarmData> {

    @Select("<script>select * from historical_alarm_data " +
            " <where> " +
            " <if test=\"defenseSection != null and defenseSection != ''\">and defense_section like concat('%',#{defenseSection},'%') </if> " +
            " <if test=\"endTime != null and endTime != ''\">and end_time &lt;= #{endTime} </if> " +
            " <if test=\"startTime != null and startTime != ''\"> and start_time &gt;= #{startTime} </if> " +
            " </where> " +
            "order by id desc</script>")
    List<HistoricalAlarmData> get(@Param("defenseSection") String defenseSection,@Param("startTime") String startTime, @Param("endTime") String endTime);

    @Insert("insert into historical_alarm_data(defense_section,status,voltageA,currentA,voltageB,currentB,voltageC,currentC," +
            "high_rated_voltage,rated_voltage,rated_current,alarm_level,alarm_status,start_time,end_time,solve_status,mark_id,statusA,statusB,statusC) " +
            "values (#{defenseSection},#{status},#{voltageA},#{currentA},#{voltageB},#{currentB},#{voltageC},#{currentC}," +
            "#{highRatedVoltage},#{ratedVoltage},#{ratedCurrent},#{alarmLevel},#{alarmStatus},#{startTime},#{endTime},#{solveStatus}," +
            "#{MarkId},#{statusA},#{statusB},#{statusC})")
    int add(HistoricalAlarmData record);

    @Delete("<script>delete from historical_alarm_data where id in <foreach collection=\"ids\" item=\"item\" open=\"(\" separator=\",\" close=\")\">#{item} </foreach> </script>")
    void delete(@Param("ids") Integer[] ids);

//    @Update("update historical_alarm_data set end_time=#{data.end_time},solve_status=#{data.solveStatus} where id = #{id}")
    @Update({"<script> UPDATE historical_alarm_data",
            "<set>",
            "<if test='data.end_time != null'>end_time = #{data.end_time},</if>",
            "<if test='data.solveStatus != null'>solve_status = #{data.solveStatus},</if>",
            "</set>",
            "where id = #{id} </script>"})
    int update(HistoricalAlarmData data, Integer id);

    @Select("<script>select * from historical_alarm_data " +
            "where defense_section=#{defenseSection} and alarm_level=#{alarmLevel} and alarm_status=#{alarmStatus} " +
            "order by start_time desc LIMIT 1 </script>")
    HistoricalAlarmData getOneAlarmData(@Param("defenseSection")String defenseSection, @Param("alarmLevel")String alarmLevel, @Param("alarmStatus")String alarmStatus);

}




